﻿//
// Created by 50177 on 2019/7/8.
//

#include <include/Communication/Protocol.h>
#include <windows.h>
#include <QtCore/qeventloop.h>
#include <QDebug>
#include <QThread>

#include <QtSerialPort/QSerialPortInfo>
#include<QMutexLocker>
#include "include/Parameter/BVParamReader.h"
#include "include/Widget/BinocularCamreaWidget.h"


constexpr UINT8 HEAD_START_BYTE = 0xEB;
constexpr UINT8 HEAD_TX_BYTE = 0xA0;
constexpr UINT8 HEAD_RX_BYTE = 0xA1;
constexpr int CMD_LENGTH = 12;

constexpr int SLOW_AXIS_MAX_FACTOR = 65536;

constexpr int MAX_CMD_NUM_IN_BUFFER = 100;

constexpr UINT8 GET_MOTOR_POS_CMD = 0x30;

Protocol* Protocol::instance = nullptr;
QThread* Protocol::worker = nullptr;

//quint8 Protocol::normalize(quint8 val, quint8 min, quint8 max) {
//	if (val < min) {
//		val = min;
//	}
//
//	if (val > max) {
//		val = max;
//	}
//
//	return val;
//}

Protocol* Protocol::getInstance() {

	if (!instance) {
		instance = new Protocol();
	}

	return instance;
}

//void Protocol::reconnect() {
//
//	LOG4CPLUS_INFO(QBVLog::Instance()->Log4cplusLogger(), "Protocol::reconnect serialport>>>>>>>>>>>>>>>>>>>>>>>>>>> b");
//
//	//readCount = 0;
//
//	//serialPort->close();
//
//	//if (!serialPort->open(QIODevice::ReadWrite)) {
//
//	//	LOG4CPLUS_ERROR(QBVLog::Instance()->Log4cplusLogger(), "  Protocol::reconnect connect to oct device failed");
//
//	//	worker->quit();
//	//}
//
//	LOG4CPLUS_INFO(QBVLog::Instance()->Log4cplusLogger(), "Protocol::reconnect serialport>>>>>>>>>>>>>>>>>>>>>>>>>>> e");
//
//}

void Protocol::clearBuffer()
{
	if (nullptr!=serialPort) {
		serialPort->clear();
	}
}

void Protocol::stop() {

	//LOG4CPLUS_INFO(QBVLog::Instance()->Log4cplusLogger(), "Protocol::stop()");

	if (worker) {
		worker->quit();
		worker->wait();
		delete worker;
		worker = nullptr;
		if (instance) {
			delete instance;
			instance = nullptr;
		}
	}
}

void Protocol::connectPort(QString & portname) {

	stop();

	//auto portInfo = std::make_unique<QSettings>(Config_Path, QSettings::IniFormat);
	//auto portCOM = portInfo->value(USBPORTLIST).toString();
	//auto portlist = SettingDialog::getCameraDevices().getUSBPorts();
	auto portName = portname;
	//qDebug() << "Protocol::connectPort <<USBPORTLIST>>>>>>>" << portCOM;
	//auto portName = QString("COM%1").arg(portCOM.toInt());
	qRegisterMetaType<Request>("Request");
	qDebug() << "COM portName>>>>>>>>>>>>> " << portName;

	//qDebug() << "QSerialPortInfo::availablePorts():COM>>>>>>>>>>" << QSerialPortInfo::availablePorts();
	Q_FOREACH(QSerialPortInfo port, QSerialPortInfo::availablePorts())		//获取可连接的所有串口的名字
	{			
		qDebug() << "QSerialPortInfo::availablePorts():" << port.portName();
		//QSerialPortInfo对象表示一个串行端口，可以查询端口名称、系统位置、描述和制造商。
		if (port.portName() == portName)
		{
			if (instance == nullptr)//added by qinyaning 202007241625
				instance = new Protocol();
			instance->portName = portName;
			if (worker == nullptr)//added by qinyaning 202007241625
				worker = new QThread();
			instance->moveToThread(worker);//调用QThread::exec()触发事件循环

			//link signal and slot
			qDebug() << "线程worker work:>>>>>>>>>>>>>>>";
			connect(worker, SIGNAL(started()), instance, SLOT(init()), Qt::QueuedConnection);
			connect(worker, SIGNAL(finished()), instance, SLOT(destroy()), Qt::DirectConnection);

			connect(instance, SIGNAL(sendRequest(const Request&)), instance,
				SLOT(sendSlot(const Request&)), Qt::QueuedConnection);

			worker->start();
			break;//added by qinyaing 202007211135
		}
	}
}

Protocol::Protocol() {
	available = false;
	readCount = 0;
	readBuff = new char[CMD_LENGTH];

	//connect(this, &Protocol::SigShowBvError, QBvErrorDialog::Instance(),
	//	&QBvErrorDialog::OnShowBvError, Qt::QueuedConnection);//added by qinyaning 202008120949
}

Protocol::~Protocol() {
	if (readBuff) {
		delete[] readBuff;
		readBuff = nullptr;
	}
	if (_requestCmdBuff) {
		delete[] _requestCmdBuff;
		_requestCmdBuff = nullptr;
	}
	if (serialPort) {
		serialPort->close();
		delete serialPort;
		serialPort = nullptr;
	}

}

Response& Protocol::send(const Request &request) {								//send(Request)
	qDebug() << "Response& Protocol::send>>>>>>>>>>>>>>>>";
	QMutexLocker reqLocker(&mutexReq);
	static Response tmpCurrentResponse(Response::Type::NoResponse);				//实例化Response

	if (serialPort && available) 
	{
		QMutexLocker resqLocker(&mutexResp);
									
		_currRequestMotorRpm = request.getRpm();	
		qDebug() << "Response& Protocol::send _currRequestMotorRpm:>>>>>>>>>>>>" << _currRequestMotorRpm;
		
		qDebug() << "Response& Protocol::sendRequest:>>>>>>>>>>>>";
		emit sendRequest(request);

		//if (!waitRespCond.wait(&mutexResp, _delayTime)) 
		//{
		//	LOG4CPLUS_ERROR(QBVLog::Instance()->Log4cplusLogger(), "Protocol::send can not get response bytesAvailable: "
		//		<< serialPort->bytesAvailable() << " "
		//		<< serialPort->error() << " "
		//		<< _delayTime);


			//serialPort->clear();
		//	if (nullptr != request.getMotor()) {
		//		int errorCode = BVERROR_MOTOR_X_TIMEOUT + request.getMotor()->getMotorID();
		//		int detailInforCode = request.getMotor()->getMotorID() + (BVError::GET_X_TIMEOUT - OctMotor::MotorX);
		//		auto msg = BVError::QBVErrorInfor::Instance()->CurrentMotorErrorStr(static_cast<BVError::MOTOR_ERROR_CODE>(detailInforCode));
		//		LOG4CPLUS_ERROR(QBVLog::Instance()->Log4cplusLogger(), "Error: " << errorCode << " Errormsg: " << msg.toStdString());
		//	}
			//--决定唤醒阻塞的线程
			//waitRespCond.wakeAll();//added by qinyaning 202007150928
			//LOG4CPLUS_WARN(QBVLog::Instance()->Log4cplusLogger(), "Protocol::send wake all threads");
			//return tmpCurrentResponse;
		//}
	}

	//if (nullptr != currentResponse) {
	//	return *currentResponse;
	//}
	return tmpCurrentResponse;
}

//--added by qinyaning 202007131517
void Protocol::readSlot() {
	qDebug() << "Protocol::readSlot>>>>>>>>>>>>>>";
	if (serialPort && available) {
		QMutexLocker respLocker(&mutexResp);
		if (readCount == 0) 
		{
			std::memset(readBuff, 0, CMD_LENGTH);
		}
		readCount = readCount + serialPort->read((readBuff + readCount), CMD_LENGTH - readCount);
		if (readCount == CMD_LENGTH) 
		{
			readCount = 0;
			static Response tmpResponse;
			tmpResponse.InitParams();
			if (readBuff[0] == (char)HEAD_START_BYTE && readBuff[1] == (char)HEAD_RX_BYTE) 
			{
				if (readBuff[4] == (char)GET_MOTOR_POS_CMD/*0x30*/) {
					double val = (unsigned char)readBuff[7] * SLOW_AXIS_MAX_FACTOR/*65536*/ + (unsigned char)readBuff[8] * 256 +
						(unsigned char)readBuff[9];
					val = val * _currRequestMotorRpm / 32;
					tmpResponse.SetType(Response::Type::Query_Motor_Result);
					tmpResponse.setMotorResult(val);

				}
				else if (readBuff[3] == (char)0xfe) {
					tmpResponse.SetType(Response::Type::Query_Board_Version_Result);
					tmpResponse.setMajorVersion(readBuff[7]);
					tmpResponse.setMinorVersion(readBuff[8]);
					tmpResponse.setPatchVersion(readBuff[9]);
				}
				else {
					tmpResponse.SetType(Response::Type::ACK);
					if (readBuff[5] == 0x00) {
						tmpResponse.setExecuted(false);				//executed = false不执行

						//LOG4CPLUS_ERROR(QBVLog::Instance()->Log4cplusLogger(), "Protocol::readSlot cmd not executed");

					}
					else if (readBuff[5] == 0x01) {
						tmpResponse.setExecuted(true);
					}
				}
			}
			else 
			{
				//LOG4CPLUS_ERROR(QBVLog::Instance()->Log4cplusLogger(), "Protocol::readSlot get error resp head");
			}

			currentResponse = &tmpResponse;
			waitRespCond.wakeAll();//为了唤醒所有的调用Send所有的线程
		}
	}
	else
	{
		//LOG4CPLUS_ERROR(QBVLog::Instance()->Log4cplusLogger(), "Protocol::readSlot avaible: " << available);
	}
}

void Protocol::sendSlot(const Request& request) 
{
	if (serialPort) {														//输入的是端口
		switch (request.getType()) {
		/*case Request::Type::Motor_Stop:
			createStopRequest(*request.getMotor());
			break;
		case Request::Type::Motor_Move_Infinite:
			createMoveInfiniteRequest(*request.getMotor(), request.getDirection());
			break;
		case Request::Type::Motor_Move_Absolute:
			createMoveAbsRequest(*request.getMotor(), request.getTargetLocation());
			break;
		case Request::Type::Motor_Move_Relative:
			createMoveRelRequest(*request.getMotor(), request.getDirection(), request.getRelativeLocation());
			break;
		case Request::Type::Motor_Query_Location:
			createQueryPositionRequest(*request.getMotor());
			break;
		case Request::Type::Top_Board_Version_Query:
		case Request::Type::Bottom_Board_Version_Query:
		case Request::Type::DAC_Board_Version_Query:
			createQueryBoardVersionRequest(request);*/
			//break;
		case Request::Type::Eye_Light_Control:
			createEyeLightRequest(request);
			break;
		//case Request::Type::SLD_Control:
		//	createSLDRequest(request);
		//	break;
		//case Request::Type::OLED_Position_Control:
		//	createOLEDRequest(request);
		//	break;
		//case Request::Type::Mirror_Reset:
		//	createMirrorResetRequest(request);
		//	break;
		//case Request::Type::Mirror_Mode_Control:
		//	createMirrorModeControlRequest(request);
		//	break;
		//case Request::Type::Mirror_Axis_Fast_Length_Control:
		//	createMirrorAxisFastLengthControlRequest(request);
		//	break;
		//case Request::Type::Mirror_Axis_Slow_Length_Control:
		//	createMirrorAxisSlowLengthControlRequest(request);
		//	break;
		}

		int c;
		quint16 check = 0;
		for (c = 2; c < CMD_LENGTH - 2; c++) {
			check = check + (quint8)_requestCmdBuff[c];
		}

		_requestCmdBuff[CMD_LENGTH - 1] = check & 0xff;
		_requestCmdBuff[CMD_LENGTH - 2] = (check >> 8) & 0xff;
		serialPort->write(_requestCmdBuff, CMD_LENGTH);

	}
}

void Protocol::destroy() {
	available = false;
	//waitRespCond.notify_all();
	if (serialPort && serialPort->isOpen()) {
		serialPort->close();
		delete serialPort;
		serialPort = nullptr;
	}
}

void Protocol::serialPortError(QSerialPort::SerialPortError error) {
	//if (error != QSerialPort::NoError)
		//LOG4CPLUS_ERROR(QBVLog::Instance()->Log4cplusLogger(), "encounter qserial port error: error content: " << error);
}

void Protocol::init() 
{

	serialPort = new QSerialPort();
	connect(serialPort, &QIODevice::readyRead, this, &Protocol::readSlot, Qt::QueuedConnection);
	connect(serialPort, &QSerialPort::errorOccurred, this, &Protocol::serialPortError, Qt::QueuedConnection);
	serialPort->setPortName(portName);

	if (!serialPort->open(QIODevice::ReadWrite)) {

		//LOG4CPLUS_ERROR(QBVLog::Instance()->Log4cplusLogger(), " Protocol::init connect to oct device failed");
		qDebug() << "串口打开失败,请重新选择" << endl;

		worker->quit();
	}
	else {
		qDebug() << "串口打开成功" << endl;
		available = true;
		serialPort->setBaudRate(QSerialPort::Baud115200);							//设置波特率
		serialPort->setStopBits(QSerialPort::StopBits::OneStop);					//一位停止位
		serialPort->setParity(QSerialPort::Parity::NoParity);						//保存奇偶校验模式，无校验位
		serialPort->setDataBits(QSerialPort::DataBits::Data8);						//保存帧中的数据位为8位
		serialPort->setReadBufferSize(CMD_LENGTH* MAX_CMD_NUM_IN_BUFFER);			//12*100 

		emit onInit();
	}
}

//--added by qinyaning. 202007130940
//bool Protocol::createMirrorAxisFastLengthControlRequest(const Request& request) {
//	bool res = true;
//
//	initRequestBuff();
//
//	_requestCmdBuff[2] = 0x02;
//	_requestCmdBuff[3] = 0x02;
//
//	qint16 startVol = /*65536*/SLOW_AXIS_MAX_FACTOR * (request.getAxisFastStart() + 10.733) / (2.5 * 8.5074);
//	qint16 endVol = /*65536*/SLOW_AXIS_MAX_FACTOR * (request.getAxisFastEnd() + 10.733) / (2.5 * 8.5074);
//	_requestCmdBuff[6] = startVol >> 8;
//	_requestCmdBuff[7] = startVol & 0xff;
//	_requestCmdBuff[8] = endVol >> 8;
//	_requestCmdBuff[9] = endVol & 0xff;
//	switch (request.getMode()) {
//	case OctMode::Mode::Single:
//	case OctMode::Mode::Single_Preview:
//	case OctMode::Mode::Area:
//	case OctMode::Mode::Radial:
//	case OctMode::Mode::Single_Front:
//	case OctMode::Mode::Radial_Front:
//		_requestCmdBuff[4] = 0x60;
//		_requestCmdBuff[5] = 0x00;
//		break;
//	case OctMode::Mode::Enface:
//		_requestCmdBuff[4] = 0x61;
//		_requestCmdBuff[5] = 0x00;
//		break;
//	case OctMode::Mode::GlaucomaDisc:
//		_requestCmdBuff[4] = 0x62;
//		_requestCmdBuff[5] = 0x00;
//		break;
//	case OctMode::Mode::GlaucomaMacula:
//		_requestCmdBuff[4] = 0x62;
//		_requestCmdBuff[5] = 0x00;
//		break;
//	}
//
//	return res;
//}

//bool Protocol::createMirrorAxisSlowLengthControlRequest(const Request& request) {
//	bool res = true;
//	initRequestBuff();
//
//	_requestCmdBuff[2] = 0x02;
//	_requestCmdBuff[3] = 0x02;
//
//	qint16 startVol = /*65536*/SLOW_AXIS_MAX_FACTOR * (request.getAxisSlowStart() + 10.777) / (2.5 * 8.5399);
//	qint16 endVol = /*65536*/SLOW_AXIS_MAX_FACTOR * (request.getAxisSlowEnd() + 10.777) / (2.5 * 8.5399);
//	_requestCmdBuff[6] = startVol >> 8;
//	_requestCmdBuff[7] = startVol & 0xff;
//	_requestCmdBuff[8] = endVol >> 8;
//	_requestCmdBuff[9] = endVol & 0xff;
//	switch (request.getMode()) {
//	case OctMode::Mode::Area:
//		_requestCmdBuff[4] = 0x60;
//		_requestCmdBuff[5] = 0x01;
//		break;
//	case OctMode::Mode::Enface:
//		_requestCmdBuff[4] = 0x61;
//		_requestCmdBuff[5] = 0x01;
//		break;
//	case OctMode::Mode::GlaucomaDisc:
//		_requestCmdBuff[4] = 0x62;
//		_requestCmdBuff[5] = 0x01;
//		break;
//	case OctMode::Mode::GlaucomaMacula:
//		_requestCmdBuff[4] = 0x62;
//		_requestCmdBuff[5] = 0x01;
//		break;
//	}
//
//	return res;
//}

//bool Protocol::createMirrorModeControlRequest(const Request& request) {
//	bool res = true;
//	initRequestBuff();
//
//	quint16 calCycle = (request.getCycle() - 2) * 30 - 1;
//	qint16 calVol;
//	_requestCmdBuff[2] = 0x02;
//	_requestCmdBuff[3] = 0x00;
//	_requestCmdBuff[8] = calCycle >> 8;
//	_requestCmdBuff[9] = calCycle & 0xff;
//	switch (request.getMode()) {
//	case OctMode::Mode::Single:
//		_requestCmdBuff[4] = 0x41;
//		break;
//	case OctMode::Mode::Area:
//		_requestCmdBuff[4] = 0x42;
//		break;
//	case OctMode::Mode::Enface:
//		_requestCmdBuff[4] = 0x43;
//		break;
//	case OctMode::Mode::Radial:
//		_requestCmdBuff[4] = 0x44;
//		break;
//	case OctMode::Mode::GlaucomaDisc:
//		_requestCmdBuff[4] = 0x45;
//		break;
//	case OctMode::Mode::GlaucomaMacula:
//		_requestCmdBuff[4] = 0x45;
//		break;
//	case OctMode::Mode::Single_Front:
//		_requestCmdBuff[4] = 0x46;
//		break;
//	case OctMode::Mode::Radial_Front:
//		_requestCmdBuff[4] = 0x47;
//		break;
//	case OctMode::Mode::Single_Preview:
//		_requestCmdBuff[4] = 0x49;
//		break;
//	}
//
//	//LOG4CPLUS_INFO(QBVLog::Instance()->Log4cplusLogger(), "getMode: " << request.getMode() << " in createMirrorModeControlRequest");
//
//	if (request.getMirrorDirection() == OctMode::Horizontal) {
//		_requestCmdBuff[5] = 0x00;//设置横向扫描
//		calVol = ((request.getVoltage() + 10.777) * SLOW_AXIS_MAX_FACTOR/*65536*/) / (2.5 * 8.5399);
//	}
//	else {
//		_requestCmdBuff[5] = 0x02;//设置纵向扫描
//		calVol = ((request.getVoltage() + 10.733) * SLOW_AXIS_MAX_FACTOR/*65536*/) / (2.5 * 8.5074);
//	}
//	_requestCmdBuff[6] = calVol >> 8;
//	_requestCmdBuff[7] = calVol & 0xff;
//
//	if (request.getMirrorTrigger() == OctMode::SignalTrigger::CSLO) {
//		_requestCmdBuff[5] = _requestCmdBuff[5] + 0x01;
//	}
//	return res;
//}

//bool Protocol::createMirrorResetRequest(const Request&/*request*/) {
//	bool res = true;
//	initRequestBuff();
//
//	_requestCmdBuff[2] = 0x02;
//	_requestCmdBuff[3] = 0x00;
//	_requestCmdBuff[4] = 0x40;
//
//	return res;
//}

//bool Protocol::createOLEDRequest(const Request& request) {
//	bool res = true;
//	initRequestBuff();
//
//	_requestCmdBuff[2] = 0x00;
//	_requestCmdBuff[3] = 0x03;
//	_requestCmdBuff[4] = 0x71;
//	_requestCmdBuff[8] = normalize(request.getOLED_X(), 0x00, 0xff);
//	_requestCmdBuff[9] = normalize(request.getOLED_Y(), 0x00, 0xff);
//	return res;
//}

//bool Protocol::createSLDRequest(const Request& request) {
//	bool res = true;
//	initRequestBuff();
//
//	_requestCmdBuff[2] = 0x00;
//	_requestCmdBuff[3] = 0x02;
//	_requestCmdBuff[4] = 0x70;
//	if (request.getLightOn()) {
//		_requestCmdBuff[5] = 0x00;
//		_requestCmdBuff[9] = request.getSldLevel();
//	}
//	else {
//		_requestCmdBuff[5] = 0x01;
//		_requestCmdBuff[9] = 0x00;
//	}
//
//	return res;
//}

void Protocol::initRequestBuff() {
	if (_requestCmdBuff == nullptr) {
		_requestCmdBuff = new char[CMD_LENGTH];
	}
	std::memset(_requestCmdBuff, 0, CMD_LENGTH);
	_requestCmdBuff[0] = HEAD_START_BYTE;					//constexpr UINT8 HEAD_START_BYTE = 0xEB;
	_requestCmdBuff[1] = HEAD_TX_BYTE;
}

//bool Protocol::createQueryBoardVersionRequest(const Request& request) {
//	bool res = true;
//	initRequestBuff();
//	switch (request.getType()) {
//	case Request::Type::Top_Board_Version_Query:
//		_requestCmdBuff[2] = 0x00;
//		break;
//	case Request::Type::Bottom_Board_Version_Query:
//		_requestCmdBuff[2] = 0x01;
//		break;
//	case Request::Type::DAC_Board_Version_Query:
//		_requestCmdBuff[2] = 0x02;
//		break;
//	}
//	_requestCmdBuff[3] = (char)0xFE;
//	return res;
//}

//bool Protocol::createMotorAddress(const OctMotor& motor) {
//	bool res = true;
//	initRequestBuff();
//	switch (motor.getMotorID()) {
//	case OctMotor::MotorX:
//		_requestCmdBuff[2] = 0x01;
//		_requestCmdBuff[3] = 0x00;
//		break;
//	case OctMotor::MotorY:
//		_requestCmdBuff[2] = 0x01;
//		_requestCmdBuff[3] = 0x01;
//		break;
//	case OctMotor::MotorZ:
//		_requestCmdBuff[2] = 0x01;
//		_requestCmdBuff[3] = 0x02;
//		break;
//	case OctMotor::MotorE://额托电机
//		_requestCmdBuff[2] = 0x01;
//		_requestCmdBuff[3] = 0x03;
//		break;
//	case OctMotor::MotorG://光程电机
//		_requestCmdBuff[2] = 0x00;
//		_requestCmdBuff[3] = 0x00;
//		break;
//	case OctMotor::MotorQ://屈光电机
//		_requestCmdBuff[2] = 0x00;
//		_requestCmdBuff[3] = 0x01;
//		break;
//	default:
//		break;
//	}
//
//	return res;
//}
//
//void Protocol::sendGetMotorTimeoutStatus(char motorType1, char motorType2) {//added by qinyaning 202009151427
//	int index = 0;
//
//	if (motorType1 == 0x01 && motorType2 == 0x00) {
//		index = 0;
//	}
//
//	if (motorType1 == 0x01 && motorType2 == 0x01) {
//		index = 1;
//	}
//
//	if (motorType1 == 0x01 && motorType2 == 0x02) {
//		index = 2;
//	}
//
//	if (motorType1 == 0x01 && motorType2 == 0x03) {
//		index = 3;
//	}
//
//	if (motorType1 == 0x00 && motorType2 == 0x00) {
//		index = 4;
//	}
//
//	if (motorType1 == 0x00 && motorType2 == 0x01) {
//		index = 5;
//	}
//
//	//emit SigMotorPosesGetTimeOutStatus(index, true);
//	if (BVGui_Select::getInstance()->PhotocouplerStatusDetector()) {
//		BVGui_Select::getInstance()->PhotocouplerStatusDetector()->SetMotorPosesGetTimeOutStatus(index, true);
//	}
//}
//
bool Protocol::createEyeLightRequest(const Request& request) {
	bool res = true;
	initRequestBuff();

	_requestCmdBuff[2] = 0x02;
	_requestCmdBuff[3] = 0x01;
	_requestCmdBuff[4] = 0x50 + request.getEyeLightNumber();		//eyeLightNumber
	if (request.getLightOn()) {										
		_requestCmdBuff[5] = 0x00;
	}
	else {
		_requestCmdBuff[5] = 0x01;
	}

	return res;
}

//bool Protocol::createMoveRelRequest(const OctMotor& motor, OctMotor::Direction direction, double val) {
//	bool res = true;
//	createMotorAddress(motor);
//
//	_requestCmdBuff[4] = 0x21;
//	if (direction == OctMotor::Direction::Positive) {
//		_requestCmdBuff[5] = 0x00;
//	}
//	else if (direction == OctMotor::Direction::Negative) {
//		_requestCmdBuff[5] = (char)0x80;
//	}
//
//	int count = 2 * val * 16.0 / motor.getRpm();
//	_requestCmdBuff[7] = count >> 16;
//	_requestCmdBuff[8] = (count & 0xff00) >> 8;
//	_requestCmdBuff[9] = count & 0xff;
//
//	return res;
//}
//
//bool Protocol::createMoveAbsRequest(const OctMotor& motor, double val) {
//	bool res = true;
//	createMotorAddress(motor);
//	_requestCmdBuff[4] = 0x20;
//	int count = 2 * val * 16.0 / motor.getRpm();
//	_requestCmdBuff[7] = count >> 16;
//	_requestCmdBuff[8] = (count & 0xff00) >> 8;
//	_requestCmdBuff[9] = count & 0xff;
//
//	if (OctMotor::High == motor.getSpeed()) {
//		_requestCmdBuff[5] = 0x01;
//	}
//	else {
//		_requestCmdBuff[5] = 0x00;
//	}
//
//	return res;
//}
//
//bool Protocol::createQueryPositionRequest(const OctMotor& motor) {
//	bool res = true;
//	createMotorAddress(motor);
//	_requestCmdBuff[4] = 0x30;
//
//	return res;
//}
//
//bool Protocol::createStopRequest(const OctMotor& motor) {
//	bool res = true;
//	createMotorAddress(motor);
//	_requestCmdBuff[4] = 0x2F;
//
//	return res;
//}
//
//bool Protocol::createMoveInfiniteRequest(const OctMotor& motor, OctMotor::Direction direction) {
//	bool res = true;
//	createMotorAddress(motor);
//	_requestCmdBuff[4] = 0x22;
//	if (direction == OctMotor::Direction::Positive) {
//		_requestCmdBuff[5] = 0x00;
//	}
//	else if (direction == OctMotor::Direction::Negative) {
//		_requestCmdBuff[5] = (char)0x80;
//	}
//
//	return res;
//}
////--
